package com.boot.plus.config.web;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer, ApplicationContextAware{
	//spring容器,切勿别注入
	private ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	//静态资源配置
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		
	}
	
	//默认servlet请求
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	
	//配置视图解析器
	@Bean("viewResolver")
	public InternalResourceViewResolver createViewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		//注入spring容器
		viewResolver.setApplicationContext(this.applicationContext);
		//关闭缓存
		viewResolver.setCache(false);
		//设置路径
		viewResolver.setPrefix("/WEB-INF/html/");
		//扩展名
		viewResolver.setSuffix(".html");
		
		return viewResolver;
	}
	
	
	
	
	
	
	
	
	

}
